package org.rupa.core.register;

import com.google.gson.Gson;
import org.rupa.common.constant.HttpPathConstant;
import org.rupa.common.model.HeartbeatBag;
import org.rupa.common.model.RegistryInfo;
import org.rupa.common.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

/**
 * 处理所有的与注册中心的服务
 */
public abstract class AbstractHttpRegister extends AbstractRegisterHeartbeatClient {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    protected RegistryInfo registryInfo;

    @Override
    protected void register() {
        try {
            int i;
            for (i = 0; i < 3; i++) {
                // 重试机制
                ResponseEntity<String> responseEntity = HttpUtil.request(restTemplate, getURL(HttpPathConstant.REGISTER), registryInfo.getServiceInfo());

                if (responseEntity.getStatusCode() == HttpStatus.OK) {
                    log.info("注册完成");
                    break;
                } else {
                    // 暂停两秒后继续
                    Thread.sleep(2 * 1000);
                }

                serverLashUpdateTime = System.currentTimeMillis();
            }

            if (i == 3) {
                close("注册失败");
            }
        } catch (Exception e) {
            close(e.getMessage());
        }
    }

    @Override
    protected void unregister() {
        try {
            ResponseEntity<String> responseEntity = HttpUtil.request(restTemplate, getURL(HttpPathConstant.UNREGISTER), registryInfo.getServiceInfo());

            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                close("注销失败");
            }
        } catch (Exception e) {
            close(e.getMessage());
        }
    }

    @Override
    protected void refreshHeartbeat() {
        try {
            int i;
            for (i = 0; i < 3; i++) {
                // 重试机制
                ResponseEntity<String> responseEntity = HttpUtil.request(restTemplate, getURL(HttpPathConstant.REFRESH_HEARTBEAT),
                        HeartbeatBag
                                .builder()
                                .mode("send")
                                .serviceInfo(registryInfo.getServiceInfo())
                                .build());

                if (responseEntity.getStatusCode() == HttpStatus.OK) {
                    break;
                } else {
                    Thread.sleep(2 * 1000);
                }

                serverLashUpdateTime = System.currentTimeMillis();
            }

            if (i == 3) {
                close("发送心跳失败");
            }
        } catch (Exception e) {
            close(e.getMessage());
        }
    }

    @Override
    protected void close(String msg) {
        log.error(msg);
        // 服务器宕机 退出代码
        SpringApplication.exit(context);
    }

    private String getURL(String path) {
        return "http://" + registryInfo.getServerIp() + path;
    }
}
